

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import '../controller/DatabaseListController.dart';
import '../dao/broadcaster_dao.dart';
import '../model/Author.dart';
import '../services/database_service.dart';

class CustomBottomSheeet extends StatelessWidget {

  final Author author;
  final Function follow,block,report;
  const CustomBottomSheeet({super.key,
     required this.author, required this.follow, required this.block, required this.report});

  @override
  Widget build(BuildContext context) {

    final DatabaseService _databaseService = DatabaseService.instance;
    final DatabaseController _databaseListController = Get.find<DatabaseController>();

    return SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Visibility(
              visible: !(author.isFriend??false),
              child: ListTile(
                  title: const Center(child: Text('Follow')),
                  onTap: () {
                    BroadCasterDao.addFriend(
                        int.parse(author.userId!), () {
                      _databaseService.updateAuthorFriend(
                          int.parse(author.userId!), true);
                      follow();
                      Get.back();
                    });
                  })),
          ListTile(
              title: const Center(
                child: Text('Block'),
              ),
              onTap: () {
                BroadCasterDao.block(int.parse(author.userId!),
                        () {
                      Get.back();
                      _databaseListController.fetchCreationData();
                      _databaseListController.fetchMediaData();
                      Fluttertoast.showToast(
                          msg: 'Block Successfully',
                          toastLength: Toast.LENGTH_LONG,
                          gravity: ToastGravity.CENTER);
                    });
              }),
          ListTile(
              title: const Center(
                child: Text('Report'),
              ),
              onTap: () {
                Get.back();
                _showSecondBottomSheet(context);
              }),
          ListTile(
              title: const Center(
                child: Text('Cancel'),
              ),
              onTap: () {
                Get.back();
              }),
        ],
      ),
    );
  }

  void _showSecondBottomSheet(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                _reportItem('Pornographic'),
                _reportItem('False gender'),
                _reportItem('Fraud'),
                _reportItem('Political sensitive'),
                _reportItem('Other'),
                ListTile(
                    title: const Center(
                      child: Text('Cancel'),
                    ),
                    onTap: () {
                      Get.back();
                    }),
              ],
            ),
          );
        });
  }

  Widget _reportItem(String complainSub) {
    return ListTile(
        title: Center(child: Text(complainSub)),
        onTap: () {
          BroadCasterDao.report(int.parse(author!.userId!), complainSub, () {
            Get.back();
            Fluttertoast.showToast(
                msg: 'Report Successfully',
                toastLength: Toast.LENGTH_LONG,
                gravity: ToastGravity.CENTER);
          });
        });
  }
}
